#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;


int tandembcycle(vector<int> red, vector<int> blue, bool fastest){
    sort(red.begin(), red.end());    
    sort(blue.begin(), blue.end());
    int result = 0;
    int x = 0;
    int y = fastest ? blue.size() - 1 : 0; 
    for (; x < red.size(); x++, y--){
        if (red[x] <= blue[y]){
            result += blue[y];  
            
        }
        else{
            result += red[x];
        }
    }   
    return result;
}

int main(int argc, char const *argv[])
{
    vector<int> red = {5, 3, 2, 5, 9};
    vector<int> blue = {3, 6, 2, 7, 1};
    
    int nnn = tandembcycle(red, blue, true);

    cout<<nnn<<endl;
    
    return 0;
}
